
//文件基本名称 银行存款.docx
export function getFileBaseName(fileName: string) {
    if (!fileName) return fileName;
    let fileNameArr = fileName.replaceAll(/\\/g, '/').split("/");
    return fileNameArr[fileNameArr.length - 1]
}
//文件基本名称,不包含扩展: 银行存款.docx => 银行存款
export function getFileBaseNameOnly(fileName: string) {
    if (!fileName) return fileName;
    let fileBaseName = getFileBaseName(fileName);
    if (fileBaseName.indexOf(".") == -1) return fileName;
    let fileExt = getFileExtName(fileBaseName);
    return fileBaseName.substring(0, fileBaseName.length - fileExt.length);//
}
//文件扩展名, 如.docx
export function getFileExtName(fileName: string) {
    if (!fileName) return fileName;
    let fileBaseName = getFileBaseName(fileName);
    if (fileBaseName.indexOf(".") == -1) return "";
    let fileNameArr = fileBaseName.split(".");
    return '.' + fileNameArr[fileNameArr.length - 1];//
}
/**
 * 改文件扩展名
 * @param fileName abc.docx
 * @param ext  .pdf
 * @returns  abc.pdf
 */
export function changeFileExt(fileName: string, ext: string) {
    if (!fileName) return fileName;
    let oldExt = getFileExtName(fileName);
    let ret = fileName.substring(0, fileName.length - oldExt.length) + ext;
    return ret;
}
//交换文件名
/**
 * 
 * @param fromFileName 
 * @param toFileName 要返回的
 * @returns 
 */
export function changeFileBaseName(fromFileName: string, toFileName: string) {

    let fromFileBaseName = getFileBaseName(fromFileName);
    let toBaseFileName = getFileBaseName(toFileName);
    return toFileName.substring(0, toFileName.length - toBaseFileName.length) + fromFileBaseName;
}
/**
 * 获取上级路径,linux格式是: ${appPath}/ProjectData/2021120117623/函证模板文件/
 * @param path 
 * @returns  ${appPath}/ProjectData/2021120117623/,带文件分隔符号的
 */
export function getParentPath(path: string): string {
    // 获取倒数第二个 '/' 的索引
    const lastSlashIndex = path.lastIndexOf('/', path.length - 2);

    // 如果没有找到路径中的 '/', 则返回 null
    if (lastSlashIndex <= 0) {
        return "";
    }
    // 返回上级目录的路径（确保路径以 '/' 结尾）
    const parentPath = path.substring(0, lastSlashIndex + 1) + '/';

    return parentPath.replace("//", "/");
}
/**
 * 获取文件夹
 * @param fileFullName  c:/abc/test.txt => a:/abc/
 * @returns 
 */
export function getFilePath(fileFullName: string): string {
    // 获取倒数第二个 '/' 的索引
    if (!fileFullName) return fileFullName;
    const lastSlashIndex = fileFullName.lastIndexOf('/', fileFullName.length - 2);
    return fileFullName.substring(0, lastSlashIndex);
}